<template>
  <el-main>
    <!-- Main content -->
    <!-- 搜索栏 -->
    <el-form ref="searchForm" :model="searchForm" :inline="true" size="small">
      <el-form-item prop="courseName">
        <el-input v-model="searchForm.courseName" placeholder="请输入课程名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color:#FF7670;" icon="el-icon-close" @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 课程表格列表 -->
    <el-table :data="tableList" :height="tableHeight" border stripe>
      <el-table-column align="center" prop="courseName" label="课程名称" />
      <el-table-column align="center" prop="courseYear" label="开课年份" />
      <el-table-column align="center" prop="teacherName" label="授课老师" />
      <el-table-column align="center" prop="point" label="成绩" />
      <el-table-column align="center" prop="type" label="开课学期">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==='0'" type="success" size="small">春季</el-tag>
          <el-tag v-if="scope.row.type==='1'" type="danger" size="small">秋季</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </el-main>

</template>

<script>
import { getCourseListApi } from '@/api/stu'
import { getUserId } from '@/utils/auth'
export default {
  data() {
    return {
      // 搜索参数设置
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        courseName: '',
        stuId: getUserId()
      },
      // 表格属性
      tableHeight: 0,
      // 表格数据
      tableList: []
    }
  },
  created() {
    this.getCourseList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  methods: {
    async getCourseList() {
      const res = await getCourseListApi(this.searchForm)
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    },
    // 页容量变化
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getCourseList()
    },
    // 当前页变化
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getCourseList()
    },
    // 搜索按钮
    searchBtn() {
      this.getCourseList()
    },
    // 重置按钮
    resetBtn() {
      this.searchForm.courseName = ''
      this.searchForm.currentPage = 1
      this.searchForm.pageSize = 10
      this.getCourseList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
